package com.partners.service.Impl;

import com.github.binarywang.utils.qrcode.QrcodeUtils;
import com.google.gson.Gson;
import com.partners.configure.QiniuConfig;
import com.partners.constant.ErrorMessageEnum;
import com.partners.model.BusinessException;
import com.partners.service.UploadFileService;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FileInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author liangyongle
 * @date 2018-12-12
 */
@Service
@Slf4j
public class UploadFileServiceImpl implements UploadFileService {


    @Resource
    private QiniuConfig qiniuConfig;

    @Override
    public String uploadFile(byte[] uploadBytes) {
        UploadManager uploadManager = qiniuConfig.getUploadManager();
        String upToken = qiniuConfig.getUploadToken();
        try {
            //默认不指定key的情况下，以文件内容的hash值作为文件名
            Response response = uploadManager.put(uploadBytes, null, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return "/" + putRet.hash;
        }catch (IOException e) {
            log.error("上传文件错误:", e);
            throw BusinessException.of(ErrorMessageEnum.OPERATION_FAILURE);
        }
    }

    @Override
    public String uploadFile(File file, String key) {
        UploadManager uploadManager = qiniuConfig.getUploadManager();
        String upToken = qiniuConfig.getUploadToken();
        try {
            Response response = uploadManager.put(file, key, upToken);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return putRet.key;
        } catch (IOException e) {
            log.error("上传文件错误:", e);
            throw BusinessException.of(ErrorMessageEnum.OPERATION_FAILURE);
        }
    }


    @Override
    public String getFileInfo(String key) {
        BucketManager bucketManager = qiniuConfig.getBucketManager();
        try {
            FileInfo fileInfo = bucketManager.stat(qiniuConfig.getBucket(), key);
            if(fileInfo!=null){
                return key;
            }else {
                return  null;
            }
        } catch (QiniuException ex) {
            System.err.println(ex.response.toString());
        }
        return null;
    }


    /**
     * 生成二维码
     * @Param url
     * return String
     *
     * */
    @Override
    public String createQrcode(String url) {
//        String path ="/images/d/logoz.png";
//        byte[] paths = QrcodeUtils.createQrcode(url,10, new File(path));
        byte[] paths = QrcodeUtils.createQrcode(url, null);
        String qrcode = qiniuConfig.getDomain()+"/"+this.uploadFile(paths);
        return qrcode;
    }

    @Override
    public String createQrcode1(String url) {
        try {
            File file = ResourceUtils.getFile("classpath:static/images/logoz.png");
            byte[] paths = QrcodeUtils.createQrcode(url,300, file);
            //byte[] paths = QrcodeUtils.createQrcode(url, null);
            String qrcode = qiniuConfig.getDomain()+"/"+this.uploadFile(paths);
            return qrcode;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


  return null;

    }
}
